// 模拟客户端逻辑
class ClientLogic {
  constructor() {
    this.animationController = null;
  }

  // 校验资源并触发动画
  validateAndAnimate(playerResources) {
    if (playerResources.ingredients >= 1) {
      this.triggerAnimation();
      return true;
    }
    return false;
  }

  triggerAnimation() {
    console.log('触发烹饪动画');
  }
}

// 模拟服务端逻辑
class ServerLogic {
  constructor() {
    this.database = new Database();
  }

  // 服务端验证并更新数据库
  verifyAndUpdate(playerId, recipe) {
    const player = this.database.getPlayer(playerId);
    if (this.checkIngredients(player, recipe)) {
      this.deductIngredients(player, recipe);
      const earnings = this.calculateEarnings(recipe);
      this.database.updatePlayer(playerId, earnings);
      return earnings;
    }
    return null;
  }

  checkIngredients(player, recipe) {
    return player.ingredients >= recipe.ingredientsRequired;
  }

  deductIngredients(player, recipe) {
    player.ingredients -= recipe.ingredientsRequired;
  }

  calculateEarnings(recipe) {
    return recipe.baseEarnings;
  }
}

// 模拟数据库
class Database {
  constructor() {
    this.players = {};
  }

  getPlayer(playerId) {
    return this.players[playerId] || {
      id: playerId,
      ingredients: 0,
      gold: 0,
      satisfaction: 0,
      level: 1,
      experience: 0
    };
  }

  updatePlayer(playerId, earnings) {
    const player = this.getPlayer(playerId);
    player.gold += earnings;
    player.satisfaction += 10;
    this.players[playerId] = player;
  }
}

// 模拟订单处理流程
class OrderProcessor {
  constructor() {
    this.database = new Database();
    this.recipes = [
      { id: 1, ingredientsRequired: 1, baseEarnings: 10, cookingTime: 5 }
    ];
  }

  processOrder(playerId, order) {
    const player = this.database.getPlayer(playerId);
    const recipe = this.matchRecipe(order);
    if (recipe && this.checkInventory(player, recipe)) {
      this.startCooking(recipe);
      const { earnings, experience } = this.calculateRewards(recipe);
      this.updatePlayerStats(playerId, earnings, experience);
      return true;
    }
    return false;
  }

  matchRecipe(order) {
    return this.recipes.find(recipe => recipe.id === order.recipeId);
  }

  checkInventory(player, recipe) {
    return player.ingredients >= recipe.ingredientsRequired;
  }

  startCooking(recipe) {
    console.log(`开始烹饪，倒计时 ${recipe.cookingTime} 秒`);
  }

  calculateRewards(recipe) {
    return { earnings: recipe.baseEarnings, experience: 10 };
  }

  updatePlayerStats(playerId, earnings, experience) {
    const player = this.database.getPlayer(playerId);
    player.gold += earnings;
    player.experience += experience;
    if (player.experience >= 100) {
      player.level += 1;
      player.experience -= 100;
    }
    this.database.players[playerId] = player;
  }
}

// 示例使用
const client = new ClientLogic();
const server = new ServerLogic();
const orderProcessor = new OrderProcessor();

// 玩家点击烹饪
const playerResources = { ingredients: 2 };
if (client.validateAndAnimate(playerResources)) {
  const earnings = server.verifyAndUpdate(1, { ingredientsRequired: 1, baseEarnings: 10 });
  if (earnings) {
    console.log(`金币增长: ${earnings}`);
  }
}

// 处理订单
const order = { recipeId: 1 };
if (orderProcessor.processOrder(1, order)) {
  console.log('订单处理完成');
}

module.exports = {
  ClientLogic,
  ServerLogic,
  Database,
  OrderProcessor
};